package com.hmeter.hadoop.hbase.gui;

import java.awt.BorderLayout;
import javax.swing.BorderFactory;

import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledTextField;

import com.hmeter.hadoop.hbase.HBasePutSampler;

public class HBasePutSamplerGui extends AbstractSamplerGui {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2374804642435404945L;

	private JLabeledTextField zookeeperQuorumTextField = null;
	private JLabeledTextField hbaseTableTextField = null;
	private JLabeledTextField rowKeyTextField = null;
	private JLabeledTextField fullColumnQualifiersTextField = null;
	private JLabeledTextField filterColumnFamiliesTextField = null;
	private JLabeledTextField delimiterTextField = null;
	
	public HBasePutSamplerGui() {
		super();
		init();
	}
	
	public void init() {
		setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH);
            
        zookeeperQuorumTextField = new JLabeledTextField("Zookeeper quorum");
        hbaseTableTextField = new JLabeledTextField("HBase table");
        rowKeyTextField = new JLabeledTextField("Row key");
        fullColumnQualifiersTextField = new JLabeledTextField("Full column qualifiers list");
        filterColumnFamiliesTextField = new JLabeledTextField("Filter column families");
        delimiterTextField = new JLabeledTextField("Delimiter");

        //main panel
        VerticalPanel panel = new VerticalPanel();
        panel.setBorder(BorderFactory.createEtchedBorder());
 
        panel.add(zookeeperQuorumTextField);
        panel.add(hbaseTableTextField);
        panel.add(rowKeyTextField);     
        panel.add(fullColumnQualifiersTextField);
        panel.add(filterColumnFamiliesTextField);
        panel.add(delimiterTextField);
        
        add(panel, BorderLayout.CENTER);
	}
	
	@Override
	public void clearGui()
	{
		super.clearGui();
		zookeeperQuorumTextField.setText("");
		hbaseTableTextField.setText("");
		rowKeyTextField.setText("");
		fullColumnQualifiersTextField.setText("");
		filterColumnFamiliesTextField.setText("");
		delimiterTextField.setText("");
	}
	
	@Override
	public String getLabelResource() {
		return getClass().getSimpleName();
	}
	
	@Override
	public String getStaticLabel() {
	    return "HBase Put Sampler";
	}
	
	@Override
	public TestElement createTestElement() {
		HBasePutSampler hbasePutSampler = new HBasePutSampler();
		modifyTestElement((TestElement) hbasePutSampler);
		return (TestElement) hbasePutSampler;
	}
	
	@Override
	public void modifyTestElement(TestElement element) {
		super.configureTestElement(element);
		if (element instanceof HBasePutSampler)
		{
			HBasePutSampler hbasePutSampler = (HBasePutSampler) element;
			hbasePutSampler.setZookeeperQuorum(zookeeperQuorumTextField.getText());
			hbasePutSampler.setHBaseTable(hbaseTableTextField.getText());
			hbasePutSampler.setRowKey(rowKeyTextField.getText());
			hbasePutSampler.setFullColumnQualifiers(fullColumnQualifiersTextField.getText());
			hbasePutSampler.setFilterColumnFamilies(filterColumnFamiliesTextField.getText());
			hbasePutSampler.setDelimiter(delimiterTextField.getText());
		}
	}
	
	@Override
	public void configure(TestElement element)
	{
		super.configure(element);
		if (element instanceof HBasePutSampler)
		{
			HBasePutSampler hbasePutSampler = (HBasePutSampler) element;
			zookeeperQuorumTextField.setText(hbasePutSampler.getZookeeperQuorum());
			hbaseTableTextField.setText(hbasePutSampler.getHBaseTable());
			rowKeyTextField.setText(hbasePutSampler.getRowKey());
			fullColumnQualifiersTextField.setText(hbasePutSampler.getFullColumnQualifiers());
			filterColumnFamiliesTextField.setText(hbasePutSampler.getFilterColumnFamilies());
			delimiterTextField.setText(hbasePutSampler.getDelimiter());
		}
	}
}
